package com.trkj.usercenter.charts;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.newwork.ProgressBarDataFromRemote;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图表统计：作为父类使用
 * Created by LiZanhong on 2015/06/25.
 */
public class ChartFragment extends Fragment{
    private View root;
    private ChartBarsUtils chartBarsUtils;

    private String chartUrl;
    private String title;
    private String key;
    private String value;

    public String getChartUrl() {
        return chartUrl;
    }

    public void setChartUrl(String chartUrl) {
        this.chartUrl = chartUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getUrl(){
        return MessageFormat.format(getChartUrl(),
                UserService.getInstance(this.getActivity()).parseUser().userId
                );
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        chartBarsUtils = new ChartBarsUtils();
        chartBarsUtils.setTitle(getTitle());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(root == null){
            root = inflater.inflate(R.layout.fragment_chart_bar, null);
            final BarChart barChart = (BarChart) root.findViewById(R.id.chart);

            ProgressBarDataFromRemote dataFromRemote =
                    new ProgressBarDataFromRemote(getActivity());
            dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
                @Override
                public void onResponseResult(String result) {
                    JSONObject jsonObject = JSON.parseObject(result);
                    if(jsonObject.getBoolean("success")){
                        JSONArray data = jsonObject.getJSONArray("data");
                        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                        for(int i = 0; i < data.size(); i ++){
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("label", data.getJSONObject(i).getString(getKey()));
                            map.put("value", data.getJSONObject(i).getInteger(getValue()));
                            list.add(map);
                        }

                        BarData barData = chartBarsUtils.getBarData(list);
                        chartBarsUtils.showChart(barChart, barData);
                    }
                }
            });

            dataFromRemote.execute(getUrl());
        }

        return root;
    }


}
